#include <iostream>
#include <unistd.h>

#include "Comm.hpp"

int main()
{

    char buffer[1024];
    SetNonBlock(0);
    while (true)
    {
        // printf("Enter# ");
        // fflush(stdout);
        ssize_t n = ::read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            printf("echo# %s", buffer);
        }
        else if (n == 0)
        {
            printf("read done\n");
            break;
        }
        else
        {
            if (errno == EWOULDBLOCK)
            {
                //方便测试
                sleep(2);
                std::cout << "底层数据没有就绪，开始轮询检测" << std::endl;
                //do other thing
                std::cout << "可以做其他事情" << std::endl;
                continue;
            }
            else if(errno == EINTR)
            {
                //继续进行读取，直到没有数据
                continue;
            }
            else
            {
                perror("read\n");
                break;
            }


            // perror("read");
            // printf("n = %ld\n", n);
            // printf("errno = %d\n", errno);
            // break;
        }
    }
    return 0;
}





